﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ToolGab.Classes;
using System.Threading;
using System.IO;

namespace ToolGab.Renamer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            Splasher.Status = "Creating the main form...";
            InitializeComponent();

            mainMenuFileExit.Image = ToolGab.Ressources.GetImage("ApplicationExit");

            //Thread.Sleep(100);
            Splasher.Close();
        }

        private void LoadFilesList(DirectoryInfo dirInfo)
        {
            lvwFiles.Items.Clear();
            try
            {
                foreach (FileInfo fi in dirInfo.GetFiles(txtFilter.Text))
                {
                    ListViewItem lvi = new ListViewItem(fi.Name);
                    lvi.Text = fi.Name;
                    lvwFiles.Items.Add(lvi);
                }
            }
            catch (Exception)
            {
                
                
            }
            // When all extension are load into lvwExt, we make sure that all the column width are 
            // properly sized.
            for (int i = 0; i < lvwFiles.Columns.Count; i++)
            {
                lvwFiles.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }

        }

        private void tvfFolders_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag is DirectoryInfo)
            {
                DirectoryInfo di = (DirectoryInfo)e.Node.Tag;
                LoadFilesList(di);
            }
            else
            {
                LoadFilesList(null);
            }
            
        }

        private void txtFilter_Leave(object sender, EventArgs e)
        {
            if (tvfFolders.SelectedNode.Tag is DirectoryInfo)
            {
                DirectoryInfo di = (DirectoryInfo)tvfFolders.SelectedNode.Tag;
                LoadFilesList(di);
            }
            else
            {
                LoadFilesList(null);
            }
            
        }

        private void mainMenuFileExit_Click(object sender, EventArgs e)
        {
            ApplicationExit();
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// End properly the application
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void ApplicationExit()
        {
            // Save the default settings
            Properties.Settings.Default.Save();
            // Exit the application.
            Application.Exit();
        }
    }
}
